package com.sta.oldman.coder;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class TcpHandler extends SimpleChannelInboundHandler<TcpMsg> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TcpMsg msg) {
        System.out.println("接收到完整消息: " + msg);
        System.out.println("头部: " + msg.getHeader());
        System.out.println("尾部: " + msg.getTail());
        System.out.println("校验和: " + msg.getSum());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        System.err.println("处理消息时发生异常: " + cause.getMessage());
        ctx.close();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        System.out.println("客户端连接成功: " + ctx.channel().remoteAddress());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        System.out.println("客户端断开连接: " + ctx.channel().remoteAddress());
    }
}
